/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ClientServerInreraction;
import com.sun.mail.smtp.SMTPTransport;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.Authenticator;
/**
 *
 * @author Иван
 */
public class SendMail {
 private String d_email = "saintjoint@gmail.com",//"ADDRESS@gmail.com",
            d_password = "infinity_ice",//Pass
            d_host = "smtp.gmail.com",
            d_port = "465",
            m_to = "i.petrushincs@gmail.com",
            m_subject = "Testing",
            m_text = "Hey, this is the testing email.";
    private Properties props;

    public SendMail() {

        props = new Properties();
        props.put("mail.smtp.user", d_email);
        props.put("mail.smtp.host", d_host);
        props.put("mail.smtp.port", d_port);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
        props.put("mail.smtp.socketFactory.port", d_port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
    }

    public void send() {
        SecurityManager security = System.getSecurityManager();

        try {
            Session session = Session.getInstance(props, null);

            //Authenticator authenticator = new SMTPAuthenticator();
            //session = Session.getInstance(props, authenticator );

//session.setDebug(true);

            MimeMessage msg = new MimeMessage(session);
            msg.setText(m_text);
            msg.setSubject(m_subject);
            msg.setFrom(new InternetAddress(d_email));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
           // Transport.send(msg);
            SMTPTransport t = (SMTPTransport)session.getTransport("smtp");
            t.connect(d_host, d_email, d_password);
            t.sendMessage(msg, msg.getAllRecipients());
        } catch (Exception mex) {
            mex.getMessage();
        }
    }

    /*private class SMTPAuthenticator extends javax.mail.Authenticator {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new  PasswordAuthentication(d_email, d_password);
        }

}
     *
     */
      
    

}
